package com.cmcc.driver.ui.main.viewmodel


import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cmcc.driver.data.DriverDataRepository
import com.cmcc.driver.data.DriverRequest
import com.cmcc.driver.data.Network
import com.cmcc.driver.data.entity.OrderDetail
import com.cmcc.driver.data.entity.response.OrderRecord
import com.cmcc.driver.data.entity.response.ResMsg
import com.cmcc.shared.model.AppResponse
import com.cmcc.shared.utils.L
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * Author: jiangxin
 * Date: 2019/11/15
 * Description:
 */
class OrderProgressViewModel : ViewModel() {
    val resMsg = MutableLiveData<ResMsg>()
    private val refusalSuccess = MutableLiveData<AppResponse<Boolean>>()

    var orderDetail = MutableLiveData<OrderDetail>()
    val records = MutableLiveData<OrderRecord>()

    val direction = MutableLiveData<String>()


    private val cancel = MutableLiveData<AppResponse<Boolean>>()

    /**
     * 查询正在进行的订单
     */
    fun queryOrderProgress(): MutableLiveData<OrderDetail> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().queryOrderInProgress()
                if (response.isSuccessful) {
                    val post = response.body()
                    orderDetail.value = post?.data
                } else {
                    L.d(response.errorBody().toString())
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
        return orderDetail
    }

    fun getOrderListInProgress(page: Int, pageSize: Int){
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().getOrderListInProgress(page,pageSize)
                if (response.isSuccessful) {
                    val post = response.body()
                    records.value = post?.data
                } else {
                    L.d(response.errorBody().toString())
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }


    /**
     * 司机操作下一步
     */
    fun nextOrder(
        orderId: String, payMethod: Int, mileageFee: Double,
        durationFee: Double, totalAmount: Double, status: Int
    ) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().nextOrder(
                    orderId, payMethod, mileageFee,
                    durationFee, totalAmount, status
                )
                if (response.isSuccessful) {
                    val post = response.body()
                    resMsg.value = ResMsg(post?.code ?: 0, post?.msg ?: "")
                } else {
                    L.d(response.errorBody().toString())
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }


    fun refusalOrder(orderId: String): MutableLiveData<AppResponse<Boolean>> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().refusalOrder(orderId)
                if (response.isSuccessful) {
                    val post = response.body()
                    refusalSuccess.value = post

                } else {
                    L.d(response.errorBody().toString())
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
        return refusalSuccess
    }


    fun cancel(orderId: String): MutableLiveData<AppResponse<Boolean>> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().cancel(orderId)
                if (response.isSuccessful) {
                    val post = response.body()
                    cancel.value = post

                } else {
                    L.d(response.errorBody().toString())
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
        return cancel
    }




    fun getDirection(origin: String, destination: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response =
                    Network.getInstance().googleApiService.getDirection(origin, destination).await()
                if (response.isSuccessful) {
                    val posts = response.body()
                    if (!posts?.routes.isNullOrEmpty()) {
                        direction.value = posts?.routes?.get(0)?.overview_polyline?.points
                    }
                } else {
                    L.d(response.errorBody().toString())
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    fun hasStatusChange():MutableLiveData<Boolean>{
        return DriverDataRepository.getInstance().hasStatusChange()
    }
}